/*
 * Copyright (c) 2023 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include <mc/log.h>

#include "interface/network_adapter/cooling.h"

namespace dev {

bool NetworkAdapter_Cooling::update_chip_temp_by_ncsi() {
    uint16_t chip_temp = 0;
    bool     ret =
        m_ncsi_over_mctp_huawei->get_chip_temp(DEFAULT_PACKAGE_ID, DEFAULT_CHANNEL_ID, &chip_temp);
    if (!ret) {
        elog("Error: Failed to update chip temp");
        return false;
    }
    TemperatureCelsius.set_value(static_cast<int16_t>(chip_temp));
    TemperatureStatus.set_value(chip_temp < CHIP_TEMP_INVALID ? 0 : 1);
    return true;
}

bool NetworkAdapter_Cooling::update_max_sfp_temperature(uint16_t max_temperature, uint8_t status) {
    if ((max_temperature & OM_TEMP_SIGN_BIT) == 0) {
        MaxSFPTemperatureCelsius.set_value(max_temperature & OM_TEMP_DATA_BIT);
    } else {
        MaxSFPTemperatureCelsius.set_value(-1 * (max_temperature & OM_TEMP_DATA_BIT));
    }
    MaxSFPTemperatureStatus.set_value(status);
    return true;
}

void NetworkAdapter_Cooling::start_ncsi_update_task_huawei(ncsi_over_mctp_hw_ptr ncsi_over_mctp,
                                                           mc::milliseconds      interval) {
    m_ncsi_timer            = new mc::core::timer(this);
    m_ncsi_over_mctp_huawei = ncsi_over_mctp;
    m_ncsi_timer->timeout.connect([this]() {
        update_chip_temp_by_ncsi();
    });
    m_ncsi_timer->set_single_shot(false);
    m_ncsi_timer->start(interval);
}

void NetworkAdapter_Cooling::stop_ncsi_update_task() {
    if (m_ncsi_timer) {
        m_ncsi_timer->stop();
        delete m_ncsi_timer;
        m_ncsi_timer = nullptr;
    }
    if (m_ncsi_over_mctp_huawei) {
        m_ncsi_over_mctp_huawei = nullptr;
    }
}

//  ==== smbus ====

void NetworkAdapter_Cooling::update_chip_temp_by_smbus() {
    auto request = mc::mutable_dict{{"opcode", 0x03}, {"expect_data_len", 2}};
    auto rsp     = m_smbus_obj->send_request(request);
    if (rsp.empty()) {
        elog("Error: Failed to update chip temp");
        return;
    }
    auto chip_temp = reinterpret_cast<int16_t*>(rsp.data());
    TemperatureCelsius.set_value(*chip_temp);
}

void NetworkAdapter_Cooling::start_smbus_update_task(smbus_obj_ptr    smbus_obj,
                                                     mc::milliseconds interval) {
    if (!smbus_obj) {
        elog("Error: smbus_obj is not initialized");
        return;
    }
    // 保存传入的smbus_obj到成员变量
    m_smbus_obj = smbus_obj;

    m_smbus_timer = new mc::core::timer(this);
    m_smbus_timer->timeout.connect([this]() {
        update_chip_temp_by_smbus();
    });
    m_smbus_timer->set_single_shot(false);
    m_smbus_timer->start(interval);
}

void NetworkAdapter_Cooling::stop_smbus_update_task() {
    if (m_smbus_timer) {
        m_smbus_timer->stop();
        delete m_smbus_timer;
        m_smbus_timer = nullptr;
    }
}

} // namespace dev

MC_REFLECT(dev::NetworkAdapter_Cooling)